require "common"

nDf61Mapping = {-1, -1, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, 6, 5, -1, 6, -1, 6, -1, 6, -1,
 6, -1, 6, -1, 6, 6, 5, 5, 4, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, 6
, 5, -1, 6, -1, 6, -1, 6, -1, 6, 6, 5, 6, 5, 5, 4, 4, 3, -1, 6, -1, 6, -1, 6, -1
, 6, -1, 6, -1, 6, -1, 6, 6, 5, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, 6, 5,
5, 4, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, -1, 6, 6, 5, 6, 5, 6, 5, 6, 5, 6
, 5, 5, 4, 5, 4, 4, 3, 2, 1}

--格式化字符串
function format_str( num )
	num = string.gsub( num, "*", "")
	num = string.gsub( num, "x", "")
	local out = ""
	local len = string.len( num )
	for i=1,len do
		out = out..format_num(string.sub(num,i,i))
	end
	return out
end

--=============================去除多余的0====================
function convert_str(bingo)
	if (string.len(bingo) % 2 ~= 0) then
		return nil
	end

	local target = ""
	for i=1, string.len(bingo) do
		if (i % 2 == 0) then
			target = target .. string.sub(bingo, i, i)
		end

	end
	return target
end
--数字字符串排序
function sort_str( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len do
		table.insert( st, format_num(string.sub(num,i,i)) )
	end
	table.sort(st)
	for i=1,len do
		out = out..st[i]
	end
	return out
end

--生肖排序
function sort_sx( num )
	local count = 0
	local out = ""
	local st = {}
	if not num then return 0,nil end
	local len = string.len( num )
	if len%2 ~= 0 then
		return 0,nil
	end

	for i=1,len,2 do
		c = string.sub(num,i,i+1)
		if tonumber(c) > 12 or tonumber(c) <1 then return 0,nil end
		if not in_table( c, st ) then
			table.insert( st, c )
		else
			return 0,nil
		end
		count = count+1
	end
	table.sort(st)
	for i=1,table.getn( st ) do
		out = out..st[i]
	end
	return count,out
end

--获取字符串中唯一的字符个数
function get_distinct_num( num )
	local dist = {}
	if not num then return 0 end
	local len = string.len( num )
	for i=1,len do
		c = string.sub(num,i,i)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end

	return table.getn( dist )
end

function in_table ( e, t )
	for _,v in pairs(t) do
		if (v==e) then return true end
	end
	return false
end
--===========================计算中奖个数==================================================
function ComputeBingoNumDf61(xItemstr,djStr,butType)
	local bingoNum ={0,0,0,0,0,0,0}
	local bullStr = {}
	local queryStr ={}
	local sStr = {}

	xItem = Split(xItemstr,"x")
	sStr = Split(djStr,"|")
	sStr[1] = convert_str(sStr[1])

	for i = 1,6 do
		bullStr[i] = string.sub(sStr[1],i,i)
	end
	bullStr[7] = sStr[2]

	if butType == '1'then
		for i = 1, 6 do
			queryStr[i] = string.sub(xItemstr,i,i)
		end
		queryStr[7] = xItem[2]
		for v, k in ipairs(bullStr) do
			if k == queryStr[v] then
				bingoNum[v] = 1
			else
				bingoNum[v] = 0
			end
		end
	elseif butType == '2' then
		for i = 1, 6 do
			queryStr[i] = {}
			for j = 1 ,string.len(xItem[i]) do
				queryStr[i][j] = string.sub(xItem[i],j,j)
			end
		end
		queryStr[7] = {}
		for i=1,string.len(xItem[7]), 2 do
			queryStr[7][(i+1)/2] = string.sub(xItem[7],i,i+1)
		end
		for i = 1, 7 do
			for _,k in ipairs(queryStr[i]) do
				if k == bullStr[i]  then
					bingoNum[i] = 1
				end
			end
		end
	elseif butType == '3' then
		for i=1,6 do
			queryStr[i] = {}
			queryStr[i][1] = string.sub(xItem[1],i,i)
		end
		queryStr[7] = {}
		for i=1,string.len(xItem[2]), 2 do
			queryStr[7][(i+1)/2] = string.sub(xItem[2],i,i+1)
		end
		for i = 1, 7 do
			for _,k in ipairs(queryStr[i]) do
				if k == bullStr[i]  then
					bingoNum[i] = 1
				end
			end
		end

	end
	return bingoNum
end

--===============================东方6+1单复式计算============================================================
function ComputeDf61( nMaxNum,  nBingoNum)

	local i;
	local nIndex ={}
	local nTemp = {}
	local nResult;
	local nBingo = {0,0,0,0,0,0,0}

	for i = 1, 7 do
		if (nBingoNum[i] > 1 or nMaxNum[i] < nBingoNum[i] or nBingoNum[i] > 1 or nBingoNum[i] < 0) then
			return nBingo
		end
	end
	for i0 = 1, nBingoNum[1]+1 do

		if i0 == 1 then
			nTemp[1] = combination(nMaxNum[1] - nBingoNum[1], 1);
		else
			nTemp[1] = nBingoNum[1]
		end
		if nTemp[1] ~= 0 then
			for i1 = 1, nBingoNum[2]+1 do
				if  i1 == 1 then
					nTemp[2] = combination(nMaxNum[2] - nBingoNum[2], 1)
				else
					nTemp[2] = nBingoNum[2]
				end
				if nTemp[2] ~= 0 then
					for i2 = 1, nBingoNum[3]+1 do

						if i2 == 1 then
							nTemp[3] = combination(nMaxNum[3] - nBingoNum[3], 1)
						else
							nTemp[3] = nBingoNum[3]
						end
						if nTemp[3] ~= 0 then
							for i3 = 1, nBingoNum[4]+1 do

								if i3 == 1 then
									nTemp[4] = combination(nMaxNum[4] - nBingoNum[4], 1)
								else
									nTemp[4] = nBingoNum[4]
								end
								if nTemp[4] ~= 0 then
									for i4 = 1, nBingoNum[5]+1 do

										if i4 == 1 then
											nTemp[5] = combination(nMaxNum[5] - nBingoNum[5], 1);
										else
											nTemp[5] = nBingoNum[5]
										end
										if nTemp[5] ~= 0 then
											for i5 = 1, nBingoNum[6]+1 do

												if i5 == 1 then
													nTemp[6] = combination(nMaxNum[6] - nBingoNum[6], 1);
												else
													nTemp[6] = nBingoNum[6]
												end
												if nTemp[6] ~= 0 then
													for i6 = 1, nBingoNum[7]+1 do

														if i6 == 1 then
															nTemp[7] = combination(nMaxNum[7] - nBingoNum[7], 1)
														else
															nTemp[7] = nBingoNum[7]
														end
														if nTemp[7] ~= 0 then
															nResult = 1
															for i = 1, 7 do
																nResult = nResult * nTemp[i];
															end
															--j = nQxcMapping[power(2,6)*(i6-1) + power(2,5)*(i5-1) + power(2,4)*(i4-1) + power(2,3)*(i3-1) + power(2,2)*(i2-1) + power(2,1)*(i1-1) + power(2,0)*(i0-1) + 1]
															j = nDf61Mapping[power(2,6)*(i0-1) + power(2,5)*(i1-1) + power(2,4)*(i2-1) + power(2,3)*(i3-1) + power(2,2)*(i4-1) + power(2,1)*(i5-1) + power(2,0)*(i6-1) + 1]
															if j ~= -1 then
																nBingo[j] = nBingo[j] + nResult;
															end
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
	return nBingo
end

--返回注数,倍投数 , 投注串, 指令串
function random_df61( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	--单式机选
	if butType == '1' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or not butNum or butNum > 5 or butNum < 1  or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		for i=1,butNum do
			local nums = selectNumber(6,0,1,2,3,4,5,6,7,8,9)
			local sxNum = format_num(math.random(1,12))
			zlStr = zlStr..nums[1]..nums[2]..nums[3]..nums[4]..nums[5]..nums[6].."*"..sxNum
			if i<butNum then zlStr = zlStr.."**" end
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_df61('2', zlStr)
	end

	return butNum, butPower, butStr, zlStr
end


--返回注数,倍投数,投注串
function check_df61( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#", "")
	qxStr = string.gsub( qjStr, "*", "")


	--单式直选 1#062#基本码*生肖码**基本码*生肖码#12… 062 = 6,0,0,1
	if  butType == '1' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D")  or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end

		for i=1,butNum do
			local xItem = Split( xxItem[i], "x")

			if string.len( xItem[1] ) ~= 6 or #xItem ~= 2 or string.len(xItem[2])~=2 or tonumber(xItem[2])<1 or tonumber(xItem[2])>12 then
				return 0
			else
				butStr = butStr.."00"..format_num(butPower)..format_str( xItem[1] )..xItem[2].."^"
			end
		end

	--单式自选倍投 1#0621#基本码*生肖码#倍数 0621= 6,0,1,2
	elseif butType == '2' then
		butNum = 1
		if itemLen < 1 or itemLen > 2 or string.find(qxStr,"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		local xItem = Split( itemList[1], "*")
		if string.len( xItem[1] ) ~= 6 or #xItem ~= 2 or tonumber(xItem[2])<1 or  tonumber(xItem[2])>12 then
			return 0
		else
			butStr = butStr.."00"..format_num( butPower)..format_str( itemList[1] ).."^"
		end


	--复式 1#063#基本码第一位*第二位…第六位*生肖码 063 = 6,0,0,3
	elseif butType == '3' then
		if itemLen < 1 or itemLen > 2 or string.find(qxStr,"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		local xItem = Split(itemList[1], "*")
		if table.getn(xItem) ~= 7  then
			return 0
		end

		sxCount,sxStr = sort_sx( xItem[7] )
		if sxCount == 0 then return 0 end

		butNum = sxCount

		butStr = "20"..format_num( butPower )
		for j=1,6 do
			if get_distinct_num( xItem[j]) ~= string.len(xItem[j]) then return 0 end
			butNum = butNum*get_distinct_num( xItem[j] )
			butStr = butStr..sort_str(xItem[j]).."^"
		end
		butStr = butStr..sxStr.."^"


	--生肖码复式 1#064#基本码*多个生肖码 064 = 6,0,0,4
	elseif butType == '4' then
		if itemLen < 1 or itemLen>2 or string.find(qxStr,"%D")  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		local xItem = Split(itemList[1], "*")
		if table.getn(xItem) ~= 2 or string.len(xItem[1]) ~= 6 then return 0 end

		sxCount,sxStr = sort_sx( xItem[2] )
		if sxCount < 2 or sxCount>12 then return 0 end

		butNum = sxCount
		butStr = "20"..format_num( butPower )
		for i=1,6 do
			butStr = butStr..format_num(string.sub(xItem[1],i,i)).."^"
		end
		butStr = butStr..sxStr.."^"

	else
		return 0
	end
	return butNum,butPower,butStr,inStr
end

---= =====================中奖查询=============================================================
function query_df61(butType,queryStr,djStr)
	local nBingo = {0,0,0,0,0,0}
	local butPower = 1
	local itemList = {}
	local bullStr = {}
	local bingoNum = {}
	local nMaxNum = {}
	local temp = {}
	itemList = Split( queryStr, "#" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	xxStr = string.gsub(itemList[1], "*", "x")
	xxItem = Split(xxStr, "xx")
	--====================单式自选===============================
	if butType == '1' or butType == '2' then
		nMaxNum = {1,1,1,1,1,1,1}
		for i = 1 ,table.getn(xxItem) do
			bingoNum[i] = {}
			bingoNum[i] = ComputeBingoNumDf61(xxItem[i],djStr,butType)
			temp = nBingo
			nBingo = ComputeDf61(nMaxNum,bingoNum[i])
			for j = 1, 6 do
				nBingo[j] = nBingo[j] + temp[j]
			end
		end
		for i = 1 ,6 do
			nBingo[i] = nBingo[i] * butPower
		end
	--==================复式======================
	elseif butType == '3' then
		xItem = Split(xxItem[1],"x")
		for i = 1, 6 do
			nMaxNum[i] = string.len(xItem[i])
		end
		nMaxNum[7] = string.len(xItem[7]) / 2
		bingoNum = ComputeBingoNumDf61(xxItem[1],djStr,butType)
		nBingo = ComputeDf61(nMaxNum,bingoNum)
		for i = 1 ,6 do
			nBingo[i] = nBingo[i] * butPower
		end
	--====================生肖码复式===========================
	elseif butType == '4' then
		xItem = Split(xxItem[1],"x")
		nMaxNum = {1,1,1,1,1,1}
		nMaxNum[7] = string.len(xItem[2]) / 2

		bingoNum = ComputeBingoNumDf61(xxItem[1],djStr,butType)
		nBingo = ComputeDf61(nMaxNum,bingoNum)
		for i = 1 ,6 do
			nBingo[i] = nBingo[i] * butPower
		end

	end

	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]
end
--test
--print(check_df61('1',"123456*7#123948*3"))
--print(check_df61('4',"123456*010203"))
--print(check_df61('3',"12*345*6*12*39*48*12#12"))
--print(check_df61('1',"123456*05#3"))
--for i=1,1000 do
--	print(random_df61('1',"3#12"))
--end
--print(query_df61('1',"332226*07**123456*07","010203040506|07"))
--print(query_df61('2',"132*2*3*4*5*6*07","010203040506|07"))
--print(query_df61('3',"123456*070504","010203040506|07"))
